package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.StrUtil;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.es.core.ElasticSearchTemplate;
import com.jzo2o.es.utils.SearchResponseUtils;
import com.jzo2o.foundations.model.domain.ServeAggregation;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.ServeAggregationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2024-11-03 10:18:56
 */
@Service
public class ServeAggregationServiceImpl implements ServeAggregationService {

    @Resource
    private ElasticSearchTemplate elasticSearchTemplate;

    @Override
    public List<ServeSimpleResDTO> search(String cityCode, String keyword, Long serveTypeId) {
        //在最新的ES中，建议使用Lambda表达式编程
        //创建请求对象
        SearchRequest request = new SearchRequest.Builder()
                //查询条件
                .query(query -> {
                    query.bool(bool -> {
                        bool.must(must -> {
                            //关键字查询 : 服务项名称和服务类型名称
                            if (StrUtil.isNotBlank(keyword)){
                                must.multiMatch(multiMatch -> {
                                    multiMatch.fields("serve_item_name", "serve_type_name").query(keyword);
                                    return multiMatch;
                                });
                            }
                            return must;
                        }).filter(filter ->{
                            //城市编号
                            filter.term(term ->{
                                term.field("city_code").value(cityCode);
                                return term;
                            });
                            //服务类型ID
                            if (ObjectUtils.isNotNull(serveTypeId)){
                                filter.term(term ->{
                                    term.field("serve_type_id").value(serveTypeId);
                                    return term;
                                });
                            }
                            return filter;
                        });
                        return bool;
                    });
                    return query;
                })
                //排序
                .sort(SortOptions.of(item -> {
                    item.field(field -> {
                        field.field("serve_type_sort_num").order(SortOrder.Asc)
                                .field("serve_item_sort_num").order(SortOrder.Asc);
                        return field;
                    });
                    return item;
                }))
                .build();
        //处理结果
        SearchResponse<ServeAggregation> response = elasticSearchTemplate.opsForDoc().search(request, ServeAggregation.class);
        List<ServeSimpleResDTO> dtoList = CollUtils.emptyList();
        if (SearchResponseUtils.isSuccess(response)) {
            List<Hit<ServeAggregation>> list = response.hits().hits();
            List<ServeAggregation> serveAggregationList = list.stream().map(Hit::source).collect(Collectors.toList());
            dtoList = BeanUtils.copyToList(serveAggregationList, ServeSimpleResDTO.class);
            return dtoList;
        }
        return dtoList;
    }
}